package org.xenon.vote.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.xenon.vote.model.pojo.vo.Result;
import org.xenon.vote.service.BallotService;
import org.xenon.vote.service.ChoiceService;
import org.xenon.vote.service.JudgeService;
import org.xenon.vote.util.Converter;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/edit")
public class Editor {
    private final BallotService ballotService;
    private final ChoiceService choiceService;
    private final JudgeService judgeService;

    public Editor(BallotService ballotService, ChoiceService choiceService, JudgeService judgeService) {
        this.ballotService = ballotService;
        this.choiceService = choiceService;
        this.judgeService = judgeService;


    }

    /**
     * 删除一些裁判
     *
     * @param name 目标的名字 压缩的, 换行符分割
     */
    @PostMapping("/delJudge")
    public Result delJudge(String name, HttpServletRequest req) {
        String mail = (String) req.getSession().getAttribute("user");
        List<String> err = judgeService.del(name.split("\n"), mail);
        if (err.isEmpty()) return new Result(true, "del");
        return new Result(false, err.toArray(new String[]{}));
    }

    /**
     * 添加一些裁判
     *
     * @param judge 压缩的裁判数据
     * @param turn  轮次的ID
     */
    @PostMapping("/addJudge")
    public Result addJudge(String judge, int turn, HttpServletRequest req) {
        String mail = (String) req.getSession().getAttribute("user");
        List<String> err = judgeService.add(Converter.toJudgeMap(judge), turn, mail);
        if (err.isEmpty()) return new Result(true, turn);
        return new Result(false, err.toArray(new String[]{}));
    }

    /**
     * 修改一个裁判的密码
     */
    @PostMapping("/editJudge")
    public Result editJudge(String name, String passwd, HttpServletRequest req) {
        String mail = (String) req.getSession().getAttribute("user");
        if (judgeService.edit(name, passwd, mail))
            return new Result(true, passwd);
        new Result(true, passwd);
        return new Result(true, passwd);
    }

    /**
     * @param turn    轮次的ID
     * @param options 多个选项, 一行一个
     */
    @PostMapping("/addChoice")
    public Result addChoice(int turn, String options, HttpServletRequest req) {
        String mail = (String) req.getSession().getAttribute("user");
        choiceService.addAll(turn, options.split("\n"), mail);
        return new Result(true, String.valueOf(turn));
    }

    @PostMapping("delChoice")
    public Result delChoice(int id, HttpServletRequest req) {
        String mail = (String) req.getSession().getAttribute("user");
        if (choiceService.del(id, mail))
            return new Result(true, String.valueOf(id));
        return new Result(false, String.valueOf(id));
    }

    /**
     * 编辑选项内容
     *
     * @param id     选项id
     * @param option 新的内容
     */
    @PostMapping("/editChoice")
    public Result editChoice(int id, String option, HttpServletRequest req) {
        String mail = (String) req.getSession().getAttribute("user");
        if (choiceService.editContents(id, option, mail))
            return new Result(true, option);
        return new Result(false
                , option);
    }

    /**
     * 修改投票名字
     */
    @PostMapping("/ballotName")
    public Result ballotName(int id, String name, HttpServletRequest req) {
        String mail = (String) req.getSession().getAttribute("user");
        if (!ballotService.checkByBallot(id, mail))
            return new Result( false, name);
        ballotService.editName(id, name);
        return new Result(true, name);

    }

    /**
     * 修改投票的描述
     *
     * @param id          投票的ID
     * @param description 新的描述
     */
    @PostMapping("/ballotDescription")
    public Result ballotDescription(int id, String description, HttpServletRequest req) {
        String mail = (String) req.getSession().getAttribute("user");
        if (!ballotService.checkByBallot(id, mail))
            return new Result( false, description);
        ballotService.editDescription(id, description);
            return new Result( true, description);
    }
}
